package luastring

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	lua "github.com/yuin/gopher-lua"
)

//InitLuaString 注册默认的字符串函数
func InitLuaString(l *lua.LState) {
	l.SetGlobal("sleep", l.NewFunction(Sleep))
	l.SetGlobal("split", l.NewFunction(Split))
	l.SetGlobal("fields", l.NewFunction(Fields))
	l.SetGlobal("trimSpace", l.NewFunction(TrimSpace))
	l.SetGlobal("trimPrefix", l.NewFunction(TrimPrefix))
	l.SetGlobal("trimSuffix", l.NewFunction(TrimSuffix))
	l.SetGlobal("trimRight", l.NewFunction(TrimRight))
	l.SetGlobal("trimLeft", l.NewFunction(TrimLeft))
	l.SetGlobal("hasPrefix", l.NewFunction(HasPrefix))
	l.SetGlobal("hasSuffix", l.NewFunction(HasSuffix))
	l.SetGlobal("getArgs", l.NewFunction(GetArgs))
	l.SetGlobal("getPath", l.NewFunction(GetPath))
	l.SetGlobal("ansi", l.NewFunction(Ansi))
	l.SetGlobal("utf8", l.NewFunction(Utf8))
	l.SetGlobal("pwd", l.NewFunction(Getwd))
	l.SetGlobal("ls", l.NewFunction(List))
	l.SetGlobal("pause", l.NewFunction(Pause))
	l.SetGlobal("getFileInfo", l.NewFunction(GetFileInfo))

	l.SetGlobal("Sleep", l.NewFunction(Sleep))
	l.SetGlobal("Split", l.NewFunction(Split))
	l.SetGlobal("Fields", l.NewFunction(Fields))
	l.SetGlobal("TrimSpace", l.NewFunction(TrimSpace))
	l.SetGlobal("TrimPrefix", l.NewFunction(TrimPrefix))
	l.SetGlobal("TrimSuffix", l.NewFunction(TrimSuffix))
	l.SetGlobal("TrimRight", l.NewFunction(TrimRight))
	l.SetGlobal("TrimLeft", l.NewFunction(TrimLeft))
	l.SetGlobal("HasPrefix", l.NewFunction(HasPrefix))
	l.SetGlobal("HasSuffix", l.NewFunction(HasSuffix))
	l.SetGlobal("GetArgs", l.NewFunction(GetArgs))
	l.SetGlobal("GetPath", l.NewFunction(GetPath))
	l.SetGlobal("Ansi", l.NewFunction(Ansi))
	l.SetGlobal("UTF8", l.NewFunction(Utf8))
	l.SetGlobal("GetFileInfo", l.NewFunction(GetFileInfo))

}

const luaRowsTypeName = "rows"
const luaSessionTypeName = "session"
const luaTxTypeName = "transaction"

//PopAll .
func PopAll(l *lua.LState) {
	for l.GetTop() > 0 {
		l.Pop(1)
	}
}

//GetFileInfo 获取文件信息
//返回值 table[1]修改时间 table[2]文件大小 table[3] 是否是目录
func GetFileInfo(l *lua.LState) int {
	path := l.CheckString(1)
	fi, err := os.Stat(path)
	if err != nil {
		return 0
	}
	tab := l.NewTable()
	tab.RawSetInt(3, lua.LBool(fi.IsDir()))
	tab.RawSetInt(2, lua.LNumber(fi.Size()))
	tab.RawSetInt(1, lua.LNumber(fi.ModTime().Unix()))
	l.Push(tab)
	return 1
}

//Pause 暂停
func Pause(l *lua.LState) int {
	fmt.Println("Press enter key to continue.")
	os.Stdin.Read(make([]byte, 1))
	return 0
}

//GetPath 获取程序所在路径
func GetPath(l *lua.LState) int {
	binaryPath, _ := filepath.Abs(os.Args[0])
	l.Push(lua.LString(filepath.Dir(binaryPath)))
	return 1
}

//GetArgs 获取参数
func GetArgs(l *lua.LState) int {
	n := len(os.Args)
	if n < 3 {
		return 0
	}

	tab := l.NewTable()
	for i := 2; i < n; i++ {
		tab.Insert(i-1, lua.LString(os.Args[i]))
	}
	l.Push(tab)
	return 1
}

//List 列出目录
func List(l *lua.LState) int {
	n := l.GetTop()
	if n < 1 {
		return 0
	}
	fi, err := ioutil.ReadDir(l.CheckString(1))
	if err != nil {
		return 0
	}

	tab := l.NewTable()
	for i, f := range fi {
		if f.IsDir() {
			tab.Insert(i+1, lua.LString(f.Name()+".dir"))
		} else {
			tab.Insert(i+1, lua.LString(f.Name()))
		}
	}
	l.Push(tab)
	return 1
}

//Getwd 获取当前目录
func Getwd(l *lua.LState) int {
	path, err := os.Getwd()
	if err != nil {
		l.Push(lua.LString(""))
	} else {
		l.Push(lua.LString(path))
	}
	return 1
}

func help() {
	fmt.Println()
	fmt.Println("                Email: haodreams@126.com")
	fmt.Println("                Author: Wangjun")
	fmt.Println("                LUA Version: 5.1")
	fmt.Println("========================================================")
	fmt.Println("  sleep(millisecond int)")
	fmt.Println("  split(line string, [delim string])(table)")
	fmt.Println("  fields(line string, [delim string])(table)")
	fmt.Println("  trimSpace(line string)(table)")
	fmt.Println("  trimRight(line string, prefix string)(string)")
	fmt.Println("  trimLeft(line string, suffix string)(string)")
	fmt.Println("  trimPrefix(line string, prefix string)(string)")
	fmt.Println("  trimSuffix(line string, suffix string)(string)")
	fmt.Println("  hasPrefix(line string, prefix string)(string)")
	fmt.Println("  hasSuffix(line string, suffix string)(string)")
	fmt.Println("  getArgs()(table)")
	fmt.Println("  getPath()(string)")
	fmt.Println("  ansi(string)(string)")
	fmt.Println("  utf8(string)(string)")
	fmt.Println("  pwd()(string)")
	fmt.Println("  ls(string)(table)")
	fmt.Println("  pause()")
	fmt.Println("  MapSet(key string, value string)")
	fmt.Println("  MapGet(key string) (table)")
	fmt.Println("  getFileInfo(path string) (table)")
	fmt.Println("========================================================")
}
